package com.quzi.kettle.activitise.added;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.animation.ValueAnimator;
import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.quzi.kettle.R;
import com.quzi.kettle.bean.ModeItem;
import com.quzi.kettle.utils.MSharePerference;
import com.tencent.a.a.a.a.c;
import com.tencent.a.a.a.a.g;

public class WaterDateUtil {
	static Calendar calendar = Calendar.getInstance();

	static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	// public static void deleteData(String gsonStr, Context context) {
	//
	// List<SevenSea> sevenSeas = getDataList(context);
	// List<SevenSea> newList = new ArrayList<SevenSea>();
	// for (int i = 0; i < sevenSeas.size(); i++) {
	// calendar = date2Calender(str2DateString(sevenSeas.get(i)
	// .getDateStr()));
	// calendar.add(Calendar.DAY_OF_MONTH, 7);
	// if (calendar.after(date2Calender(getCurrentTime()))) {
	// newList.add(sevenSeas.get(i));
	// }
	// }
	// String newsevenSeas = new Gson().toJson(newList);
	// MSharePerference.getInstance(context).update("SEVENSEA", newsevenSeas);
	//
	// }
	/**
	 * 刷新数据
	 * 
	 * @param context
	 */
	public static List<SevenSea> renewDataList(Context context) {
		List<SevenSea> sevenSeas = getDataList(context);
		String newsevenSeas = new Gson().toJson(getDataList(context));
		MSharePerference.getInstance(context).update("SEVENSEA", newsevenSeas);
		return sevenSeas;

	}

	/**
	 * 获取最新数据，没有保存
	 * 
	 * @param context
	 * @return
	 */
	private static List<SevenSea> getDataList(Context context) {

		List<SevenSea> sevenSeas = new Gson().fromJson(MSharePerference
				.getInstance(context).get("SEVENSEA"),
				new TypeToken<List<SevenSea>>() {
				}.getType());
		if (sevenSeas != null) {
			for (int i = 0; i < sevenSeas.size(); i++) {
				Log.v("Water", "getSave:--->" + sevenSeas.get(i).toString());
			}
		}

		List<SevenSea> newList = new ArrayList<SevenSea>();
		if (sevenSeas != null && sevenSeas.size() > 0) {
			for (int i = 0; i < sevenSeas.size(); i++) {
				Log.v("Water", "in the getDataList");
				Log.v("Water", sevenSeas.get(i).toString());
				// calendar = date2Calender(str2DateString(sevenSeas.get(i)
				// .getDateStr()));
				// // calendar.add(Calendar.DATE, 7);
				// calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + 7);
				// Log.v("Water",
				// "iftrue--->"
				// + calendar
				// .after(date2Calender(getCurrentTime())));
				// if (calendar.before(date2Calender(getCurrentTime()))) {
				// newList.add(sevenSeas.get(i));
				// }

				try {
					if (daysBetween(sevenSeas.get(i).getDateStr(),
							getCurrentTimeStr()) < 7) {
						newList.add(sevenSeas.get(i));
					}
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		if (newList != null) {
			for (int i = 0; i < newList.size(); i++) {
				Log.v("Water", "newList:--->" + newList.get(i).toString());
			}
		}
		return newList;

	}

	public static float[] getSeven(Context context) {
		List<SevenSea> lists = WaterDateUtil.renewDataList(context);

		float[] values = new float[7];
		if (lists != null && lists.size() > 0) {

			for (int i = 0; i < lists.size(); i++) {
				Log.v("Water", lists.get(i).toString());
				switch (getIndex(lists.get(i).getDateStr())) {
				case 0:
					values[0] = (float) lists.get(i).getWaterml();
					break;
				case 1:
					values[1] = (float) lists.get(i).getWaterml();
					break;
				case 2:
					values[2] = (float) lists.get(i).getWaterml();
					break;
				case 3:
					values[3] = (float) lists.get(i).getWaterml();
					break;
				case 4:
					values[4] = (float) lists.get(i).getWaterml();
					break;
				case 5:
					values[5] = (float) lists.get(i).getWaterml();
					break;
				case 6:
					values[6] = (float) lists.get(i).getWaterml();
					break;
				default:
					break;
				}
			}

		} else {
			return values;
		}

		return values;

	}

	/**
	 * 理论返回的是0 1 2 3 4 5 6 7
	 * 
	 * @param dateStr
	 * @return
	 */
	private static int getIndex(String dateStr) {
		int index = -1;
		try {
			index = daysBetween(dateStr, getCurrentTimeStr());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return index;
	}

	public static void addData(int ml, Context context) {
		List<SevenSea> sevenSeas = getDataList(context);
		Log.v("Water", "sevenSeas is null" + (sevenSeas == null) + ""
				+ "sevenSeas size--" + sevenSeas.size());
		if (sevenSeas != null && sevenSeas.size() > 0) {
			if (sevenSeas.get(sevenSeas.size() - 1).getDateStr()
					.equals(getCurrentTimeStr())) {
				sevenSeas.get(sevenSeas.size() - 1).setWaterml(
						sevenSeas.get(sevenSeas.size() - 1).getWaterml() + ml);
				Log.v("Water", "add same day");
			} else {
				sevenSeas.add(new SevenSea(getCurrentTimeStr(), ml));
				Log.v("Water", "add not same day");
			}
		} else {

			Log.v("Water", "add not record day");
			sevenSeas.add(new SevenSea(getCurrentTimeStr(), ml));
		}

		MSharePerference.getInstance(context).update("SEVENSEA",
				new Gson().toJson(sevenSeas));

	}

	private void isWeekAgo(Date mdate) {

	}

	public static Date str2DateString(String strdate) {

		try {
			return sdf.parse(strdate);
		} catch (ParseException e) {

			e.printStackTrace();
		} finally {
			return null;
		}

	}

	public static String getCurrentTimeStr() {
		Date d = new Date();

		return sdf.format(d);

	}

	public static Date getCurrentTime() {
		Date d = new Date(System.currentTimeMillis());
		return str2DateString(sdf.format(d));

	}

	private static Calendar date2Calender(Date date) {
		calendar.setTime(new java.util.Date());
		return calendar;

	}

	/**
	 * 计算两个日期之间相差的天数
	 * 
	 * @param smdate
	 *            较小的时间
	 * @param bdate
	 *            较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static int daysBetween(Date smdate, Date bdate)
			throws ParseException {

		smdate = sdf.parse(sdf.format(smdate));
		bdate = sdf.parse(sdf.format(bdate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 字符串的日期格式的计算
	 */
	public static int daysBetween(String smdate, String bdate)
			throws ParseException {

		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(smdate));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(bdate));
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}
}
